package com.zhentao.interceptor;

import com.zhentao.model.common.admin.pojo.AdminUser;
import com.zhentao.util.UserThreadLocalutil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Slf4j
public class WmTokenInterceptor implements HandlerInterceptor {
    //在方法执行之前调用 true放行 false拦截
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //获取到header里面的信息
        String adminID = request.getHeader("userId");
        System.err.println("平台管理端ID"+adminID);
        if (adminID!=null){
            AdminUser adminUser = new AdminUser();
            adminUser.setAdminUserId(Integer.valueOf(adminID));
            UserThreadLocalutil.setUser(adminUser);

            log.info("平台管理端信息放到了线程当中：",adminUser);

            log.info("平台管理端获取用户信息：",UserThreadLocalutil.getUser());
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        HandlerInterceptor.super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
    }
}
